package com.example.foo.api.web.member;

import java.io.File;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;

import com.ruoyi.common.core.domain.AjaxResult;
import com.example.support.wanquanpay.WanquanPay;
import com.example.support.wanquanpay.WanquanPayException;
import com.example.support.wanquanpay.WanquanPayFileUtils;
import com.example.foo.api.boot.TokenHelper;
import com.example.foo.ftt.model.FttUser;
import com.example.foo.ftt.service.FttUserService;
import com.example.foo.member.model.MemberIdcard;
import com.example.foo.member.service.MemberIdcardService;
import com.example.support.redis.RedisHelper;

@org.springframework.web.bind.annotation.RestController
public class MemberIdcardAction {

	protected final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(getClass());

	private final MemberIdcardService memberIdcardService;
	private final FttUserService fttUserService;
	private final WanquanPay wanquanPay;

	public MemberIdcardAction(MemberIdcardService memberIdcardService,
			FttUserService fttUserService,
			WanquanPay wanquanPay) {
		this.memberIdcardService = memberIdcardService;
		this.fttUserService = fttUserService;
		this.wanquanPay = wanquanPay;
	}

	@GetMapping("/api/memberidcards/userId={userId}")
	public AjaxResult showMemberIdcardByUserId(@PathVariable("userId") Long userId) {
		Long loginedUserId = TokenHelper.verifyToken();
		if (!Objects.equals(userId, loginedUserId)) {
			return AjaxResult.error(403, "Access Forbidden");
		}
		MemberIdcard memberIdcard = memberIdcardService.findMemberIdcardByUserId(userId);
		if (memberIdcard == null) {
			return AjaxResult.error(404, "Not Found");
		}
		return AjaxResult.successData(memberIdcard);
	}

	@PutMapping("/api/memberidcards/{id}")
	public AjaxResult updateMemberIdcard(@PathVariable("id") Long id, MemberIdcard memberIdcardForm) {
		//
		String idcard = memberIdcardForm.getIdcard();
		String idname = memberIdcardForm.getIdname();
		String picUrl1 = memberIdcardForm.getPicUrl1();
		String picUrl2 = memberIdcardForm.getPicUrl2();
		String startDate = memberIdcardForm.getStartDate();
		String endDate = memberIdcardForm.getEndDate();
		String cellphone;
		String address = memberIdcardForm.getAddress();

		//
		if (StringUtils.isBlank(idcard)) {
			return AjaxResult.error(400, "idcard must not be null");
		}
		if (StringUtils.isBlank(idname)) {
			return AjaxResult.error(400, "idname must not be null");
		}
		if (StringUtils.isBlank(picUrl1)) {
			return AjaxResult.error(400, "picUrl1 must not be null");
		}
		if (StringUtils.isBlank(picUrl2)) {
			return AjaxResult.error(400, "picUrl2 must not be null");
		}
		if (StringUtils.isBlank(startDate)) {
			return AjaxResult.error(400, "startDate must not be null");
		}
		if (StringUtils.isBlank(endDate)) {
			return AjaxResult.error(400, "endDate must not be null");
		}
		if (StringUtils.isBlank(address)) {
			return AjaxResult.error(400, "address must not be null");
		}

		//
		Long userId = TokenHelper.verifyToken();

		//
		String lockKey = String.format("lock:MemberIdcard:userId_%s", userId);
		if (!RedisHelper.setnxex(lockKey, 10, "-")) {
			return AjaxResult.error(400, "操作频繁，请稍后");
		}

		//
		MemberIdcard memberIdcard = memberIdcardService.findMemberIdcardById(id);
		if (memberIdcard == null) {
			return AjaxResult.error(404, "Not Found");
		}
		if (!Objects.equals(userId, memberIdcard.getUserId())) {
			return AjaxResult.error(403, "Access Forbidden");
		}

		//
		FttUser fttUser = fttUserService.findFttUserByUserId(userId);
		cellphone = fttUser.getCellphone();
		memberIdcardService.updateMemberIdcardBase(id, idcard, idname, picUrl1, picUrl2, startDate, endDate, cellphone, address);

		File picUrl1File = WanquanPayFileUtils.copyURLToFile(picUrl1, userId);
		File picUrl2File = WanquanPayFileUtils.copyURLToFile(picUrl2, userId);

		String outerMemberId = wanquanPay.queryMemberIdByIdCard(idcard);
		try {
			if (outerMemberId == null) {
				outerMemberId = wanquanPay.registerMember(idcard, idname, cellphone, picUrl1File, picUrl2File);
			} else {
				wanquanPay.updateMemberImagePiece(outerMemberId, cellphone, picUrl1File, picUrl2File);
			}
		} catch (WanquanPayException e) {
			return AjaxResult.error(500, e.getMessage());
		}

		//
		memberIdcardService.updateMemberIdcardSuccess(id, outerMemberId);
		fttUserService.updateFttUserIdname(fttUser.getId(), idname);

		return AjaxResult.success();
	}

}
